import axios from 'axios'
import qs from 'qs'
import { isFormData } from '@/util'

import { Message} from 'element-ui'

const req = axios.create({
    baseURL: '',
    timeout: 60000,
    responseType: 'json',
    headers: {
        'Content-Type': 'application/json; charset=UTF-8'
    }
})

// 请求拦截器
req.interceptors.request.use(
    config => {
        // config.data = (!isFormData(config.data)) ? qs.stringify(config.data) : config.data
        return config
    },
    error=> {
        Message.error('请求出错')
        return Promise.reject(error)
    })

// 响应拦截器
req.interceptors.response.use(
    response=> {
        if (response.status === 200) {
            // Message({message: '请求成功！', type: 'success'})
            return response.data
        } else {
            Message.error(response.statusText || '系统错误')
            return Promise.reject(response)
        }
    }, error=> {
        Message.error(error.message || '请求失败')
        return Promise.reject(error)
    })

export default req
